/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccstore.servlet;

import it.unitn.science.matr155863_148346_145963.bccstore.items.Commission;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author berto
 */
public class DetailsPurchaseSRV extends HttpServlet {
    
    private static Logger log = Logger.getLogger(DetailsPurchaseSRV.class);

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<div class=\"content\">");
        out.println("<h2>Order status details:</h2>");
        try {
            Integer orderState=(Integer) request.getSession().getAttribute("order_state");
            request.getSession().removeAttribute("order_state");
            Commission commission;
            switch(orderState){
                case 0:
                    commission=(Commission) request.getSession().getAttribute("commission");
                    request.getSession().removeAttribute("commission");
                    out.println("<h3>Order executed.</h3>");
                    RequestDispatcher dispatcher=request.getRequestDispatcher("/contents/user/ShowProductDetails?id="+commission.getProductId());
                    dispatcher.include(request, response);
                    showDetails(commission,out);
                    out.println("<p>Here you find the bill.</p>");
                    out.println("<a href=\""+response.encodeURL(request.getContextPath()+"/bill/"+commission.getBill())+"\">Your bill</a>");
                    break;
                case 1:                    
                    commission=(Commission) request.getSession().getAttribute("commission");
                    request.getSession().removeAttribute("commission");
                    out.println("<p>There was an error diuring the creation of your bill, contact us if you want recive the bill.</p>");
                    out.println("<a href=\"mailto:bccstore@gmail.com\">write email to us</a>");
                    break;
                case 2:
                    out.println("<h3>There was an error diuring the execution of your order.</h3>");
            }
        }catch(Exception ex){
            log.error(ex);
            RequestDispatcher dispatcher=request.getRequestDispatcher("/contents/public/errorPage.html");
            dispatcher.include(request, response);
        } finally {            
            out.println("</div>");
            //out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void showDetails(Commission commission, PrintWriter out) {
                    out.println(
                            "<table>"
                            + "<tr>"
                            + "<td>Date:</td>"
                            + "<td>"+commission.getDate()+"</td>"
                            + "</tr>"
                            + "<tr>"
                            + "<td>Price sigle item:</td>"
                            + "<td>"+commission.getPrice()+"</td>"
                            + "</tr>"
                            + "<tr>"
                            + "<td>Quantity:</td>"
                            + "<td>"+commission.getQuantity()+"</td>"
                            + "</tr>"
                            + "<tr>"
                            + "<td>Price all items:</td>"
                            + "<td>"+commission.getPrice()*commission.getQuantity()+"</td>"
                            + "</tr>"
                            + "<tr>"
                            + "<td>Seller first name:</td>"
                            + "<td>"+commission.getSellerFirstName()+"</td>"
                            + "</tr>"
                            + "<tr>"
                            + "<td>Seller last name:</td>"
                            + "<td>"+commission.getSellerLastName()+"</td>"
                            + "</tr>"
                            + "<tr>"
                            + "<td>Seller username</td>"
                            + "<td>"+commission.getSellerUserName()+"</td>"
                            + "</tr>"
                            + "</table>"
                            );
    }
}
